﻿using SqlSugar;
using System.Diagnostics;
using Zhaoxi.NET7.DbDal;

namespace Zhaoxi.NET7Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {

                #region 第一次课

                //说清ORM框架前世今生，SqlSugar闪亮登场SqlSugar轻松落地CRUD。探讨解决SqlServer数据库,高性能方案，弄清数据库SqlServer二八原则,SqlServer读写分离快速搭建， SqlSugar完美支持,SqlServer读写分离方案

                //一.SqlSugar的基本使用
                //1.SqlSugarCore 
                //2.准备链接数据库的配置 
                //3.基于SqlSugraClient去操作数据库了
                {
                    //ConnectionConfig config = new ConnectionConfig()
                    //{
                    //    DbType = DbType.SqlServer,
                    //    ConnectionString = "Data Source=PC-202206030027;Initial Catalog=CustomDBInfo;Persist Security Info=True;User ID=sa;Password=sa123",
                    //    IsAutoCloseConnection = true
                    //};
                    //using (SqlSugarClient clientclient = new SqlSugarClient(config))
                    //{
                    //    Student student = new Student()
                    //    {
                    //        Name = "秋夜",
                    //        SchoolId = 1
                    //    };

                    //    int iResult = clientclient.Insertable<Student>(student).ExecuteCommand();

                    //    Student student1 = clientclient.Queryable<Student>().OrderByDescending(c => c.Id).First();


                    //    student1.Name = "高级班的主讲老师  Richard老师";
                    //    clientclient.Updateable<Student>(student1).ExecuteCommand();

                    //    clientclient.Deleteable<Student>(student1).ExecuteCommand(); ;

                    //}
                }

                //二. 探讨ORM框架的硬伤---性能问题--多方考虑
                //1.一定是要结合业务来做优化
                //2.读写分离配置 
                //3. 程序如何支持呢?  SqlSugar轻松应对

                {
                    //ConnectionConfig config = new ConnectionConfig()
                    //{
                    //    DbType = DbType.SqlServer,
                    //    ConnectionString = "Data Source=PC-202206030027;Initial Catalog=CustomDBInfo;Persist Security Info=True;User ID=sa;Password=sa123",
                    //    IsAutoCloseConnection = true,
                    //    SlaveConnectionConfigs = new List<SlaveConnectionConfig>() //这里的这个配置就是从库的链接配置
                    //    {
                    //      new SlaveConnectionConfig(){
                    //           ConnectionString="Data Source=PC-202206030027;Initial Catalog=CustomDBInfo_new001;Persist Security Info=True;User ID=sa;Password=sa123",
                    //            HitRate=10  //查询的权重
                    //      },
                    //       new SlaveConnectionConfig(){
                    //           ConnectionString="Data Source=PC-202206030027;Initial Catalog=CustomDBInfo_new002;Persist Security Info=True;User ID=sa;Password=sa123",
                    //            HitRate=10  //查询的权重
                    //      }
                    //    }
                    //};


                    //using (SqlSugarClient clientclient = new SqlSugarClient(config))
                    //{

                    //    //for (int i = 0; i < 3; i++)
                    //    //{
                    //    //    Student student1 = clientclient.Queryable<Student>().First(c => c.Id == 21);
                    //    //} 
                    //    Student student = new Student()
                    //    {
                    //        Name = "Advance--读写分离~",
                    //        SchoolId = 1
                    //    };

                    //    int iResult = clientclient.Insertable<Student>(student).ExecuteCommand();

                    //    Student student1 = clientclient.Queryable<Student>().OrderByDescending(c => c.Id).First();
                    //    student1.Name = "高级班的主讲老师  Richard老师";
                    //    clientclient.Updateable<Student>(student1).ExecuteCommand();
                    //    clientclient.Deleteable<Student>(student1).ExecuteCommand(); ;
                    //}

                }

                //三. 如何应对数据复制延迟问题---确实有延迟
                //1. 业务妥协----找产品经理, 去沟通, 取消这种即时性的查询动作
                //2. 直接查询主库~~
                {
                    //ConnectionConfig config = new ConnectionConfig()
                    //{
                    //    DbType = DbType.SqlServer,
                    //    ConnectionString = "Data Source=PC-202206030027;Initial Catalog=CustomDBInfo;Persist Security Info=True;User ID=sa;Password=sa123",
                    //    IsAutoCloseConnection = true,
                    //    SlaveConnectionConfigs = new List<SlaveConnectionConfig>() //这里的这个配置就是从库的链接配置
                    //    {
                    //      new SlaveConnectionConfig(){
                    //           ConnectionString="Data Source=PC-202206030027;Initial Catalog=CustomDBInfo_new001;Persist Security Info=True;User ID=sa;Password=sa123",
                    //            HitRate=10  //查询的权重
                    //      },
                    //       new SlaveConnectionConfig(){
                    //           ConnectionString="Data Source=PC-202206030027;Initial Catalog=CustomDBInfo_new002;Persist Security Info=True;User ID=sa;Password=sa123",
                    //            HitRate=10  //查询的权重
                    //      }
                    //    }
                    //};


                    //bool isgoOn = true;
                    //int i = 1;
                    //using (SqlSugarClient clientclient = new SqlSugarClient(config))
                    //{
                    //    {
                    //        Student student = new Student()
                    //        {
                    //            Name = "测试延迟问题",
                    //            SchoolId = 1
                    //        };

                    //        int iResult = clientclient.Insertable<Student>(student).ExecuteCommand();
                    //    }

                    //    Stopwatch stopwatch=new Stopwatch();
                    //    stopwatch.Start();
                    //    while (isgoOn)
                    //    {

                    //        Student student = clientclient.Queryable<Student>().First(c => c.Name == "测试延迟问题");
                    //        if (student != null)
                    //        {
                    //            Console.WriteLine("数据已经查询到了~~ 不在继续查询");
                    //            break;
                    //        }
                    //        else
                    //        {
                    //            Console.WriteLine($"第{i}次查询,没有获取到结果");
                    //        }

                    //        i++;
                    //    }

                    //    stopwatch.Stop();
                    //    Console.WriteLine($"查询完毕, 共计耗时{stopwatch.ElapsedMilliseconds}毫秒~"); 
                    //}
                }

                #endregion

                #region 第二次课
                {
                    ConnectionConfig config = new ConnectionConfig()
                    {
                        DbType = DbType.SqlServer,
                        ConnectionString = "Data Source=PC-202206030027;Initial Catalog=CustomDBInfo;Persist Security Info=True;User ID=sa;Password=sa123",
                        IsAutoCloseConnection = true
                    };
                    using (SqlSugarClient clientclient = new SqlSugarClient(config))
                    {
                        //CommoditySubTableYear commodity = new CommoditySubTableYear()
                        //{
                        //    CategoryId = 1,
                        //    Createtime = DateTime.Now,
                        //    ImageUrl = "images",
                        //    Price = 344,
                        //    ProductId = 234,
                        //    Title = "欢迎大家来到.NET高级班的体验课",
                        //    Url = "URL"
                        //}; 
                        //int iResult = clientclient.Insertable<CommoditySubTableYear>(commodity).ExecuteCommand();


                        //如果数据体量过大----根据年份来分表
                        //1.2020, 2021  2022  2023年....
                        //2. 分表后----一个数据库不能有多个表同名, 根据分表的业务范围, 给表来一个后缀,用于区域


                        //按照年份自动分表
                        {
                        //    List<CommoditySubTableYear> commoditilist = new List<CommoditySubTableYear>()
                        //{
                        //    new CommoditySubTableYear()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now.AddYears(-2), //2021年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2021",
                        //        Url = "URL"
                        //    },
                        //    new CommoditySubTableYear()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now.AddYears(-1),  //2022年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2022",
                        //        Url = "URL"
                        //    },
                        //    new CommoditySubTableYear()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now,  //2023年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2023",
                        //        Url = "URL"
                        //    }

                        //};
                        //    int iResult = clientclient.Insertable<CommoditySubTableYear>(commoditilist)
                        //        .SplitTable() //操作数据的时候, 支持自动的分表
                        //        .ExecuteCommand();
                        }
                         
                        //按照月份分表
                        {
                        //    List<CommoditySubTableMonth> commoditilist = new List<CommoditySubTableMonth>()
                        //{
                        //    new CommoditySubTableMonth()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now.AddMonths(-2), //2021年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2021",
                        //        Url = "URL"
                        //    },
                        //    new CommoditySubTableMonth()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now.AddMonths(-1),  //2022年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2022",
                        //        Url = "URL"
                        //    },
                        //    new CommoditySubTableMonth()
                        //    {
                        //        CategoryId = 1,
                        //        Createtime = DateTime.Now,  //2023年
                        //        ImageUrl = "images",
                        //        Price = 344,
                        //        ProductId = 234,
                        //        Title = "欢迎大家来到.NET高级班的体验课--2023",
                        //        Url = "URL"
                        //    }

                        //};
                        //    int iResult = clientclient.Insertable<CommoditySubTableMonth>(commoditilist)
                        //        .SplitTable() //操作数据的时候, 支持自动的分表
                        //        .ExecuteCommand();
                       }


                   }


                    //能够自己定义规则吗?
                    //按照区域分---武汉---上海--北京..... 
                    //自己来扩展
                    // 扩展规则----实现一个接口
                    {
                        CustomSubTableDemo.ShowCustomSubTable();
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }
}